<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height="100%"固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<mescroll-uni ref="mescrollRef" @init="mescrollInit" height="100%" top="60" :down="downOption" @down="downCallback"
		:up="upOption" @up="upCallback">
		<!-- 数据列表 -->
		<good-list :list="goods"></good-list>
	</mescroll-uni>

</template>

<script>

	import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";
	import {apiSearch} from "@/api/mock.js";
	import {openComDB,executeSQL,dropSQL} from "@/pages/API/sqllite/sqllite.js";
	
	var self;
	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		components: {
			MescrollBody,
			MescrollUni
		},
		data() {
			return {
				downOption: {
					auto: false, // 不自动加载 (mixin已处理第一个tab触发downCallback)
					native: false,
					beforeEndDownScroll: 1000,
					textInOffset: "下拉刷新",
					textOutOffset: "释放刷新",
					textLoading: "加载中..."
				},
				upOption: {
					auto: false, // 不自动加载
					// page: {
					// 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 2, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 暂无数据 ~'
					},
					src: "../../static/images/totop.png",
					offset: 50,
					duration: 300,
					zIndex: 9990,
					right: 20,
					bottom: 120,
					safearea: false,
					width: 72,
					radius: "50%",
					left: null
				},
				goods: [] //列表数据
			}
		},
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			tabs: { // 为了请求数据,演示用,可根据自己的项目判断是否要传
				type: Array,
				default () {
					return []
				}
			}
		},
		methods: {
			_aaaaCCCC() {
				this.mescroll.resetUpScroll();
				this.goods=[];
			},
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
			//	console.log("请求次数");
				//联网加载数据
				self = this;
			//	console.log(this.i);
				let keyword = this.tabs[this.i].name;
				let keyindex = 0;
				//console.log(keyword);
				if (this.i == 0) {
					keyword = "未盘";
					keyindex = 0;
				} else if (this.i == 1) {
					keyword = "正常";
					keyindex = 1;
				} else if (this.i == 2) {
					keyword = "异常";
					keyindex = 2;
				} else if (this.i == 3) {
					keyword = "盘盈";
					keyindex = 3;
				} else if (this.i == 4) {
					keyword = "盘亏";
					keyindex = 4;
				}
				var sql2="select * from buildDetailTable where 1=1";
				var mydata = this.tabs[this.i].code;
				var mydataid = this.tabid;
				
				
						mydataid = uni.getStorageSync('DocumentId');
						const listdata = uni.getStorageSync("userinfo");
						
						var mydataservalue={
								PdloginName:listdata.LoginName,
								DocumentId: mydataid,
								pageSize: 20,
								_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
								pageIndex: (page.num - 1),
								"盘点结果": keyword
							}
						sql2+=" and 盘点结果='"+keyword+"'";
						sql2+=" and DocumentId='"+mydataid+"'";
						const mydataser = uni.getStorageSync("DetailSearch");
						if (mydataser == undefined) {
							mydataser = "";
						}
						if (mydataser != "") {
							mydataservalue["-资产编码"]=mydataser;
							sql2+=" and 资产编码 like '%"+mydataser+"%'";
						}
						const mydataname = uni.getStorageSync("DetailName");
						if (mydataname == undefined) {
							mydataname = "";
						}
						if (mydataname != "") {
							mydataservalue["-资产名称"]=mydataname;
							sql2+=" and 资产名称 like '%"+mydataname+"%'";
						}
						const mydatazclb = uni.getStorageSync("DetailZCLB");
						if (mydatazclb == undefined) {
							mydatazclb = null;
						}
						console.log("资产类别"+mydatazclb);
						if (mydatazclb != "" && mydatazclb!='请选择资产类别 >' && mydatazclb!='请选择资产类别') {
							//mydataservalue["AssetTypeId"]=mydatazclb;
							sql2+=" and 资产类别 like '%"+mydatazclb+"%'";
						}
						const mydatazcwz = uni.getStorageSync("DetailZCWZ");
						if (mydatazcwz == undefined) {
							mydatazcwz = null;
						}
						if (mydatazcwz != "" && mydatazcwz!=null  && mydatazcwz!='请选择资产位置 >' && mydatazcwz!='请选择资产位置') {
							//mydataservalue["StorePlaceId"]=mydatazcwz;
							sql2+=" and 资产位置 like '%"+mydatazcwz+"%'";
						}
						const mydatasybm = uni.getStorageSync("DetailSYBM");
						if (mydatasybm == undefined) {
							mydatasybm = null;
						}
						if (mydatasybm != "" && mydatasybm!=null  && mydatasybm!='请选择使用部门 >' && mydatasybm!='请选择使用部门') {
							sql2+=" and 使用部门 like '%"+mydatasybm+"%'";
						}
						const mydatasyr = uni.getStorageSync("DetailSYR");
						if (mydatasyr == undefined) {
							mydatasyr = null;
						}
						if (mydatasyr != "" && mydatasyr!=null  && mydatasyr!='请选择使用人 >' && mydatasyr!='请选择使用人') {
							sql2+=" and 使用人 like '%"+mydatasyr+"%'";
						}
						/*
						uni.setStorageSync("DetailSYBM", this.inputsybm);
						uni.setStorageSync("DetailSYR", this.inputsyr);
						*/
						mydataservalue["PdloginName"]=listdata.LoginName;
						let pageNum = page.num; // 页码, 默认从1开始
						let pageSize = page.size; // 页长, 默认每页10条
						sql2=sql2+"  limit " + pageSize + " offset " +(((pageNum-1)*pageSize)) + "";
						console.log(sql2);
						executeSQL('nfc', sql2, res => {
							//this.productList = mydata;
							// 接口返回的当前页数据列表 (数组)
							//console.log(res);
							if(res.code!=-1404){
								var mydata = [];
								//console.log(res);
								for (var i = 0; i < res.length; i++) {
									var datarow=res[i];
									let  aaaaaa=self.goods.find(x=>x.Id===datarow.Id);
									if(self.goods.indexOf(datarow)==-1){
										mydata.push(datarow);
										
									}					
																	
									}
									self.mescroll.endSuccess(mydata.length);
									//设置列表数据
									self.goods = self.goods.concat(mydata); //追加新数据
									setTimeout(() => {
										self.mescroll.endSuccess(mydata.length)
									}, 20)
								}else{
															
										self.mescroll.endSuccess(0);
											//设置列表数据
										self.goods = []; //追加新数据
											setTimeout(() => {
										self.mescroll.endSuccess(0)
											}, 20)
															
									}
							
						});
						
				

			}
		}
	}
</script>
